<template>
  <div class="content">
    <div class="header">
    <el-header><el-icon class="icon" @click="back"><ArrowLeft /></el-icon>{{ name }}申报难度录入</el-header>
  </div>
    <div class="info">
      <span>内容：{{ form.content }}</span>
      <p>编码：{{ form.code }}</p>
      <span>分值：{{ form.score }}</span>
    </div>
    <el-main>
      <el-form :model="form" label-width="100px">
        <el-form-item label="内容">
          <el-input v-model="formzan.content" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="编码">
          <el-input v-model="formzan.code" placeholder="请输入编码"></el-input>
        </el-form-item>
        <el-form-item label="分值">
          <el-input-number v-model="formzan.score" :min="0" :precision="3" :step="0.025"
            placeholder="请输入分值"></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitFormzan">添加一段</el-button>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
          <el-button @click="goToSummary" style="float: right;">查看汇总表</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import axios from 'axios';
import { useRouter, useRoute } from 'vue-router';

const route = useRoute();
const router = useRouter();

const id = route.query.id;
const name = route.query.name;
const token = localStorage.getItem('adminToken');

// 提交申报难度添加的信息
const formzan = ref({
  content: '',
  code:'',
  score:'',
  encodeId:id
});

const form = ref({
  content: '',
  code:'',
  score:'',
  encodeId:id
});

//删除最后一个加号
function removeLastPlusSign(str) {
  return str.slice(0, -1); // 从开始到倒数第二个字符
}

// 提交申报难度添加的信息  
async function submitForm() {  
  // 使用 Object.values() 和 map() 来遍历对象的值
  const newForm = {
      content: removeLastPlusSign(form.value.content),
      code: removeLastPlusSign(form.value.code),
      score: removeLastPlusSign(form.value.score),
      encodeId: form.value.encodeId
  }
  console.log(newForm);
  
  
  if (form.value.content === '' || form.value.code==='') {
    ElMessage.error('内容与编码不能为空');
    return;
  }
  if (form.value.score === 0) {
    ElMessage.error('分值不能为空');
    return;
  }
  try {
    const response = await axios.post('http://back1.idcfengye.com:15173/teamgroup/application', newForm, {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${token}`
      }
    });
    if (response.data.success) {
      ElMessage.success('添加成功');
    }
  } catch (error) {
    if (error.response) {
      ElMessage.error(`Error:${error.response.data.msg}`);
    }
  }
}

//分段提交难度录入
async function submitFormzan() {
  // 确保 formzan.value.content 不是 undefined 或 null
  if (formzan.value.content) {
    // 将现有内容和新内容用 '+' 连接起来
    form.value.content = `${form.value.content}${formzan.value.content}+`;
  }

  // 更新编码，这里假设您希望保留原有的编码，并在其后追加新的编码
  form.value.code = `${form.value.code}${formzan.value.code}+`;

  // 先将分数转换为数字，避免 NaN，并确保新分数不为 undefined 或 null
  const newScore = parseFloat(formzan.value.score) || 0;
  form.value.score = `${form.value.score === '' ? '' : form.value.score}${newScore}+`;
  
  // 其他属性可以直接覆盖，因为它们不是累积属性
  form.value.encodeId = formzan.value.encodeId;
}


// 重置表单填写内容  
const resetForm = () => {
  form.value.content = '';
  form.value.code = '';
  form.value.score = '';
  form.value.level = '';
};

function goToSummary() {
  router.push({ path: '/admin/viewapplicationdetail',query:{ id: id } });
}  

function back() {
  router.back()
}
</script>

<style scoped>
.content{
  display: flex;
  justify-content: center;
  flex-direction: column;
}
.info{
  padding: 50px;
}
.el-header {

  font-size: 30px;
  background-color: #aacec6;
  color: #FFFFFF;
  text-align: center;
  line-height: 60px;
}
.icon{
  position: absolute;
  left: 10px;
  margin-top: 1%;
}
</style>